home *** CD-ROM | disk | FTP | other *** search
- /* TNOpen.c -- XCMD to open the TN3270 driver
- copyright 1989 Cornell University
- */
-
-
- #include <Types.h>
- #include <Memory.h>
- #include <Devices.h>
- #include <HyperXCmd.h>
- #include <Errors.h>
-
- #include <String.h>
-
- #include "TNdrvr.h"
-
-
- pascal void debugger() extern 0xA9FF;
-
-
- pascal void TNOpen(hycp)
- XCmdPtr hycp;
- {
- CntrlParam drvpb;
- long * args;
- char istr[80]; /* for converting int to str */
- int istrlen;
- Str255 retstr;
- Handle strhand;
- OSErr result;
-
- if (hycp->paramCount != 4) {
- sethand(&hycp->returnValue,
- "TNOpen TNID,TNscreen,TNcursor,TNfield: need 4 globals as arguments, all quoted");
- return;
- }
-
- drvpb.ioNamePtr = "\p.TNdrvr";
- drvpb.csCode = fsCurPerm; /* same as permissions */
- PBOpen((ParmBlkPtr) &drvpb, (Boolean) 0);
-
- if (drvpb.ioResult) {
- /* can't open the TNdrvr */
- sethand(&hycp->returnValue, "Can't open .TNdrvr");
- return;
- }
-
- istrlen = shorttostr(&istr[0], drvpb.ioCRefNum);
- if (PtrToHand(&istr[0], &strhand, (Size) (istrlen + 1))) {
- sethand(&hycp->returnValue, "Out of memory");
- return;
- }
- HLock((Handle) hycp->params[0]);
- ZeroToPas(hycp, (char *) *hycp->params[0], &retstr[0]);
- SetGlobal(hycp, &retstr[0], strhand);
- if (hycp->result) {
- sethand(&hycp->returnValue, "Can't set TNID");
- return;
- }
- DisposHandle(strhand);
- HUnlock((Handle) hycp->params[0]);
-
- drvpb.csCode = HTN_OPEN;
- args = (long *) &drvpb.csParam[0];
-
- #ifdef PASSARG
- *args = (long) hycp->params[1];
- /* pass the "data ready" Handle to the drvr to pass on to TN3270 */
- #endif
-
- PBControl((ParmBlkPtr) &drvpb, (Boolean) 0);
-
- if (drvpb.ioResult) {
- /* open failed */
- switch (drvpb.ioResult) {
- case HTNR_NOTN: {
- sethand(&hycp->returnValue, "TN is not running");
- return;
- }
- case HTNR_ACTIVE: {
- sethand(&hycp->returnValue, "TN already open");
- break;
- }
- case HTNR_OPEN: {
- sethand(&hycp->returnValue, "TN already has a connection");
- return;
- }
- case badUnitErr: {
- sethand(&hycp->returnValue, "TNID is incorrect");
- return;
- }
- default: {
- sethand(&hycp->returnValue, "Unknown error");
- return;
- }
- }
- }
- /* on return first long arg set to address of screen buffer */
- istrlen = ulongtostr(&istr[0], *args);
- if (PtrToHand(&istr[0], &strhand, (Size) (istrlen + 1))) {
- sethand(&hycp->returnValue, "Out of memory");
- return;
- }
- HLock((Handle) hycp->params[1]);
- ZeroToPas(hycp, (char *) *hycp->params[1], &retstr[0]);
- SetGlobal(hycp, &retstr[0], strhand);
- if (hycp->result) {
- sethand(&hycp->returnValue, "Can't set TNscreen");
- return;
- }
- DisposHandle(strhand);
- HUnlock((Handle) hycp->params[1]);
-
- /* on return second long arg set to address of cursor ptr */
- istrlen = ulongtostr(&istr[0], *++args);
- if (PtrToHand(&istr[0], &strhand, (Size) (istrlen + 1))) {
- sethand(&hycp->returnValue, "Out of memory");
- return;
- }
- HLock((Handle) hycp->params[2]);
- ZeroToPas(hycp, (char *) *hycp->params[2], &retstr[0]);
- SetGlobal(hycp, &retstr[0], strhand);
- if (hycp->result) {
- sethand(&hycp->returnValue, "Can't set TNcursor");
- return;
- }
- DisposHandle(strhand);
- HUnlock((Handle) hycp->params[2]);
-
- /* on return third long arg set to address of field ptr */
- istrlen = ulongtostr(&istr[0], *++args);
- if (PtrToHand(&istr[0], &strhand, (Size) (istrlen + 1))) {
- sethand(&hycp->returnValue, "Out of memory");
- return;
- }
- HLock((Handle) hycp->params[3]);
- ZeroToPas(hycp, (char *) *hycp->params[3], &retstr[0]);
- SetGlobal(hycp, &retstr[0], strhand);
- if (hycp->result) {
- sethand(&hycp->returnValue, "Can't set TNfield");
- return;
- }
- DisposHandle(strhand);
- HUnlock((Handle) hycp->params[3]);
-
- return;
- }
-
-
- sethand(thand, str)
- Handle * thand;
- char * str;
- {
- if (*thand == NULL) {
- *thand = NewHandle((Size) 0);
- }
- SetHandleSize(*thand, (long) (strlen(str) + 1));
- strcpy(**thand, str);
- }
-
-
- /* convert a short to a string, returning the length */
-
- shorttostr(strp, thenum)
- char * strp;
- short thenum;
- {
- char tarr[80];
- char * tarrp = &tarr[0];
- char * strstart = strp;
-
- if (thenum == 0) {
- /* handle zero as special case */
- *strp++ = '0';
- *strp = 0;
- return(1);
- }
- if (thenum < 0) {
- /* put in the minus sign for negative numbers */
- *strp++ = '-';
- thenum = -thenum;
- }
- *tarrp++ = 0; /* pre-terminate the backwards char array */
- while (thenum) {
- /* put the fractions into an array as decimal characters */
- *tarrp++ = (thenum % 10) + '0';
- thenum /= 10;
- }
- /* now read the array backwards into the arg */
- while (*--tarrp) {
- *strp++ = *tarrp;
- }
- *strp = 0; /* terminate the arg array */
- return(strp - strstart); /* and return the length */
- }
-
-
- /* convert a long to a string, returning the length */
-
- ulongtostr(strp, thenum)
- char * strp;
- long thenum;
- {
- char tarr[80];
- char * tarrp = &tarr[0];
- char * strstart = strp;
-
- if (thenum == 0) {
- /* handle zero as special case */
- *strp++ = '0';
- *strp = 0;
- return(1);
- }
- if (thenum < 0) {
- /* put in the minus sign for negative numbers */
- *strp++ = '-';
- thenum = -thenum;
- }
- *tarrp++ = 0; /* pre-terminate the backwards char array */
- while (thenum) {
- /* put the fractions into an array as decimal characters */
- *tarrp++ = (thenum % 10) + '0';
- thenum /= 10;
- }
- /* now read the array backwards into the arg */
- while (*--tarrp) {
- *strp++ = *tarrp;
- }
- *strp = 0; /* terminate the arg array */
- return(strp - strstart); /* and return the length */
- }
-
-
- /* from xcmd.inc.c */
-
- pascal void SetGlobal(paramPtr,globName,globValue)
- XCmdPtr paramPtr; StringPtr globName; Handle globValue;
- /* Set the value of the specified HyperTalk global variable to be
- the zero-terminated string in globValue. The contents of the
- Handle are copied, so you must still dispose it afterwards. */
- {
- paramPtr->inArgs[0] = (long)globName;
- paramPtr->inArgs[1] = (long)globValue;
- paramPtr->request = xreqSetGlobal;
- (paramPtr->entryPoint)();
- }
-
- pascal void ZeroToPas(paramPtr,zeroStr,pasStr)
- XCmdPtr paramPtr; char *zeroStr; StringPtr pasStr;
- /* Fill the Pascal string with the contents of the zero-terminated
- string. You create the Pascal string and pass it in as a VAR
- parameter. Useful for converting the arguments of any XCMD to
- Pascal strings. */
- {
- paramPtr->inArgs[0] = (long)zeroStr;
- paramPtr->inArgs[1] = (long)pasStr;
- paramPtr->request = xreqZeroToPas;
- ((paramPtr->entryPoint))();
- }
-